package com.sas.web.panel;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Response;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AbstractAutoCompleteRenderer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.util.StringUtils;

import com.sas.domain.Article;
import com.sas.model.EntityModel;
import com.sas.repository.ArticleRepository;
import com.sas.web.panel.articles.ArticlesListPanel;

public class SearchPanel extends Panel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2697427926433424981L;

	private static final int MAX_CONNECTIONS_PER_SEARCH = 50;
	
	@SpringBean
	private ArticleRepository articleRepository;

	private String to;
	private List<IModel<Article>> searchList;
	
	public SearchPanel(String id) {
		super(id);
		
		Form<Void> form = new Form<Void>("form");
		
		List<IModel<Article>> articles_aux = new ArrayList<IModel<Article>>();
		for  (Article a: articleRepository.findAll()) {
			articles_aux.add(new EntityModel<Article>(Article.class, a));
		}
		
		final List<IModel<Article>> articles = articles_aux;
		AbstractAutoCompleteRenderer<IModel<Article>> autoCompleteRenderer = new AbstractAutoCompleteRenderer<IModel<Article>>() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 3289011960908027499L;

			@Override
			protected String getTextValue(IModel<Article> object) {
				return object.getObject().getCode();
			}

			@Override
			protected void renderChoice(IModel<Article> object,
					Response response, String criteria) {
				response.write(getTextValue(object));
				
			}
			
		};

		// textfield
		AbstractAutoCompleteTextField<IModel<Article>> toField = new AbstractAutoCompleteTextField<IModel<Article>>("toField", new PropertyModel<String>(this, "to"), autoCompleteRenderer) {
			/**
			 * 
			 */
			private static final long serialVersionUID = -3799794995432360916L;

			protected final List<IModel<Article>> getChoiceList(final String input) {
				return getConnectionsSubsetForSearch(articles, input);
			}

			public List<IModel<Article>> getList() {
				return searchList;
			}

			@Override
			protected String getChoiceValue(IModel<Article> choice)
					throws Throwable {
				return choice.getObject().getCode();
			}
		};

		form.add(toField);

		final Label label = new Label("selectedValue", toField.getDefaultModel());
        label.setOutputMarkupId(true);
        form.add(label);

        toField.add(new AjaxFormSubmitBehavior(form, "onchange") {

			/**
			 * 
			 */
			private static final long serialVersionUID = -168215546120542218L;

			@Override
			protected void onError(AjaxRequestTarget arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			protected void onSubmit(AjaxRequestTarget target) {
				final ArticlesListPanel articlesListPanel = new ArticlesListPanel("articlesListPanel", to, searchList);
				target.getPage().addOrReplace(articlesListPanel);
				target.addComponent(label);				
				setResponsePage(target.getPage());
				
			}
        });
		
		FeedbackPanel feedback = new FeedbackPanel("feedback");
		add(feedback);
		add(form);
	}


	protected List<IModel<Article>> getConnectionsSubsetForSearch(
			List<IModel<Article>> articles, String input) {
		List<IModel<Article>> subList = new ArrayList<IModel<Article>>();
		for (IModel<Article> art: articles) {
			if (subList.size() == MAX_CONNECTIONS_PER_SEARCH) {
				break;
			}
			
			if (StringUtils.startsWithIgnoreCase(art.getObject().getName(), input)) {
				subList.add(art);
			} else if (StringUtils.startsWithIgnoreCase(art.getObject().getCode(), input)) {
				subList.add(art);
			}
		}
		
		searchList = subList;
		return subList;
	}
}
